package br.ufpe.cin.imlm2.jbeat.model.factory;

import java.util.List;

import br.ufpe.cin.imlm2.jbeat.model.ILightWeightType;
import br.ufpe.cin.imlm2.jbeat.model.LightWeightClass;
import br.ufpe.cin.imlm2.jbeat.model.LightWeightMethod;
import br.ufpe.cin.imlm2.util.ArgumentsValidator;

public class MethodMaker {
	
	private LightWeightClass thisClass;
	
	public MethodMaker(LightWeightClass thisClass) {
		ArgumentsValidator.verifyNullness(thisClass);
		this.thisClass = thisClass;
	}
	
	public LightWeightMethod retrieveMethod(String name, List<ILightWeightType> parameterList){
		LightWeightMethod meth;
		
		if((meth = this.getMethod(name,parameterList)) == null) {
			meth = new LightWeightMethod(parameterList, name,thisClass);
			this.thisClass.addMethod(meth);
		}
		
		return meth;
	}

	private LightWeightMethod getMethod(String name,
			List<ILightWeightType> parameterList) {
		for (LightWeightMethod meth : this.thisClass.getMethods()) {
			if(meth.getName().equals(name) && meth.getParameters().size() == parameterList.size()){
				boolean matches = true;
				for (int i = 0; i < meth.getParameters().size(); i++) {
					if(meth.getParameters().get(i) != parameterList.get(i)){
						matches = false;
					}
				}
				if(matches)
					return meth;
			}
		}
		return null;
	}

}
